package com.regis.comment.aop;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.regis.comment.uilts.ThreadDataUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author 计算机系 ITAEM 纱雾休比我老婆
 * @Description 自动填充
 * @date 2022/7/28 19:23
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    //自动填充
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        //保存时自动填充创造、更新时间
        this.setFieldValByName("createTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName("updateTime",LocalDateTime.now(),metaObject);
        //保存时自动填充创造、更新用户id
        this.setFieldValByName("createUser", ThreadDataUtil.getValue(),metaObject);
        this.setFieldValByName("updateUser",ThreadDataUtil.getValue(),metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        //更新时自动填充更新时间
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        //更新时自动更新用户id
        this.setFieldValByName("updateUser",ThreadDataUtil.getValue(),metaObject);
    }
}
